package cn.snowheart.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ 的配置文件
 *
 * @author liuwanxiang
 * @version 2019/05/27
 */
@Configuration
public class RabbitMqConfiguration {

    /**
     * hello 章节的 Queue
     */
    @Bean
    public Queue helloQueue() {
        return new Queue("hello");
    }

    /**
     * object 章节的 Queue
     */
    @Bean
    public Queue userQueue() {
        return new Queue("object");
    }

    /**
     * 测试 Topic 章节
     */
    public static final String MESSAGE = "topic.message";
    public static final String MESSAGES = "topic.messages";

    @Bean
    public Queue messageQueue() {
        return new Queue(MESSAGE);
    }

    @Bean
    public Queue messagesQueue() {
        return new Queue(MESSAGES);
    }

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("topicExchange");
    }

    @Bean
    public Binding messageBinding(@Qualifier("messageQueue") Queue queue,
                                  @Qualifier("topicExchange") TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(MESSAGE);
    }

    @Bean
    public Binding messagesBinding(@Qualifier("messagesQueue") Queue queue,
                                   @Qualifier("topicExchange") TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("topic.#");
    }

    /**
     * 测试 Fanout 章节
     */
    @Bean
    public Queue fanoutQueueA(){
        return new Queue("fanout.A");
    }

    @Bean
    public Queue fanoutQueueB(){
        return new Queue("fanout.B");
    }

    @Bean
    public Queue fanoutQueueC(){
        return new Queue("fanout.C");
    }

    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanoutExchange");
    }

    @Bean
    public Binding bindingFanoutA(@Qualifier("fanoutQueueA") Queue queue,
                                  @Qualifier("fanoutExchange")FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }

    @Bean
    public Binding bindingFanoutB(@Qualifier("fanoutQueueB") Queue queue,
                                  @Qualifier("fanoutExchange")FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }

    @Bean
    public Binding bindingFanoutC(@Qualifier("fanoutQueueC") Queue queue,
                                  @Qualifier("fanoutExchange")FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }
}
